<?php
/*
	Obelix BB - Obelix Bulletin Board.
	
	Auteur : Fy - http://fee.lya.eu

	## Fonctions posts ##
*/

// Flags status
function check_status_topic($status) 
{
	if(verif_flag($status,100)) {
		$result .= _('post-it');
	}
	if(verif_flag($status,10)) {
		$result .= _('locked');
	}
	if(verif_flag($status,1)) {
		$result .= _('sondage');
	}
	if($status == 0) {
		$result .= _('normal');
	}
	return $result;
}

// Dernière réponse.
function last_rep($idt) 
{
	global $config;
	$infos_req = mysql_query('SELECT U.id AS Uid, username, date, F.id AS Fid
						  FROM  '.$config['mysql_prefix'].'messages AS F, '.$config['mysql_prefix'].'users AS U 
						  WHERE idt='.$idt.' AND U.id = F.idu
						  ORDER BY F.id DESC LIMIT 0,1');

	$infos = mysql_fetch_array($infos_req);

	$date = explode('-',$infos['date']);
	$date = $date[2].'/'.$date[1].'/'.$date[0];
	if(empty($infos['username'])) {
		$result = '-';
	} else {
		$nbm_req = mysql_query('SELECT COUNT(id)
							  FROM  '.$config['mysql_prefix'].'messages
						 	  WHERE idt='.$idt.'');
		$nbm = mysql_result($nbm_req,0);
		$page = ceil($nbm/$config['board_per_page']);
		$result = '<a href="?p=topic&amp;id='.$idt.'&amp;page='.$page.'#'.$infos['Fid'].'">'._('go').'</a> '._('par').' <a href="?p=view&amp;id='.$infos['Uid'].'">'.$infos['username'].'</a> <br />'._('le2').' '.$date;
	}

	return $result;
}

// Dernier message.
function last_topic($idf) 
{
	global $config;
	$infos_req = mysql_query('SELECT  username, U.id AS Uid, date, title, F.id AS Fid
						  FROM  '.$config['mysql_prefix'].'topics AS F, '.$config['mysql_prefix'].'users AS U 
						  WHERE idf="'.$idf.'" AND U.id=F.idu
						  ORDER BY F.uptime DESC LIMIT 0,1')or die(mysql_error());

	$infos = mysql_fetch_array($infos_req);

	$date = explode('-',$infos['date']);
	$date = $date[2].'/'.$date[1].'/'.$date[0];

	if(empty($infos['title'])) {
		$result = '-';
	} else {
		$idt_2 = $infos['Fid'];
		$infos_2_req = mysql_query('SELECT username, U.id AS Uid
							      FROM '.$config['mysql_prefix'].'messages AS F, '.$config['mysql_prefix'].'users AS U
							      WHERE idt='.$idt_2.' AND U.id = F.idu
							      ORDER BY F.id DESC LIMIT 0,1');
		$infos_2 = mysql_fetch_array($infos_2_req);
		
		if (empty($infos_2['Uid'])) {
			$username = $infos['username'];
			$uid = $infos['Uid'];
		} else {
			$username = $infos_2['username'];
			$uid = $infos_2['Uid'];
		}

		$result = '<div class="small_2"><a href="?p=topic&amp;id='.$idt_2.'">'.crop($infos['title'],30).'</a><br />'._('par').' <a href="?p=view&amp;id='.$uid.'">'.$username .'</a> '._('le2').' '.$date.'</div>';
	}

	return $result;
}

// Module des posts.
function mod_post() 
{
	global $config;
	if(check_auth()) {
		if($_GET['send']) {
			if($_GET['action'] == 'topic') {
				if(empty($_POST['title']) || empty($_POST['body'])) {
					$title = mysql_query('SELECT title FROM '.$config['mysql_prefix'].'forums 
									  WHERE id='.htmlentities($_GET['id']).'');

					$title = mysql_fetch_array($title);

					$result = '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.htmlentities($_GET['id']).'">'.$title['title'].'</a> >> '._('new_topic').'</h4>';
					$result .= _('form_vide');
				} else {
					$idf = htmlentities($_GET['id']);
					$title = htmlentities($_POST['title']);
					$body = htmlentities($_POST['body']);
					$status = $_POST['post-it']+$_POST['locked'];

					mysql_query('INSERT INTO '.$config['mysql_prefix'].'topics(idf,idu,title,body,uptime,date,status) 
							      VALUES("'.$idf.'","'.$_SESSION['idu'].'","'.$title.'","'.$body.'","'.time().'","'.date('Y-m-d').'","'.$status.'")');

					mysql_query('UPDATE '.$config['mysql_prefix'].'forums SET nbt=nbt+1 
							      WHERE id='.$idf.'');

					mysql_query('UPDATE '.$config['mysql_prefix'].'users_infos SET nbm=nbm+1 
							      WHERE idu='.$_SESSION['idu'].'');

					$result .= '<script language="javascript" type="text/javascript">
								window.location.replace("?p=forum&id='.$idf.'");
			    				</script>';
				}
			} else if ($_GET['action'] == 'repondre') {
				if(empty($_POST['body'])) {
					$title = mysql_query('SELECT T.idf AS Fidf, F.title AS Ftitle, T.title AS Ttitle 
									  FROM '.$config['mysql_prefix'].'forums AS F,  '.$config['mysql_prefix'].'topics AS T 
									  WHERE T.id='.htmlentities($_GET['id']).' AND F.id=T.idf')or die(mysql_error());
		
					$title = mysql_fetch_array($title);
					$result = '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.$title['Fidf'].'">'.$title['Ftitle'].'</a> >> <a href="?p=topic&amp;id='.htmlentities($_GET['id']).'">'.$title['Ttitle'].'</a> >> '._('repondre').'</h4>';
					$result .= _('form_vide');
				} else {
					$idt = htmlentities($_GET['id']);

					$lock_req = mysql_query('SELECT status FROM '.$config['mysql_prefix'].'topics WHERE id="'.$idt.'"');
					$lock = mysql_fetch_array($lock_req,1);
					
					if(verif_flag($lock['status'],10)) {
						$title = mysql_query('SELECT T.idf AS Fidf, F.title AS Ftitle, T.title AS Ttitle 
										FROM '.$config['mysql_prefix'].'forums AS F,  '.$config['mysql_prefix'].'topics AS T 
										WHERE T.id='.htmlentities($_GET['id']).' AND F.id=T.idf');
		
						$title = mysql_fetch_array($title);
						$result = '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.$title['Fidf'].'">'.$title['Ftitle'].'</a> >> <a href="?p=topic&amp;id='.htmlentities($_GET['id']).'">'.$title['Ttitle'].'</a> >> '._('repondre').'</h4>';
						$result .= _('lock_topic');
					} else {
						$body = htmlentities($_POST['body']);
	
						$nbmess_req = mysql_query('SELECT COUNT(id)
											FROM  '.$config['mysql_prefix'].'messages
											WHERE idt='.$idt.'');
						$nbmess = mysql_result($nbmess_req,0);

						$page = ceil($nbmess/$config['board_per_page']);

						mysql_query('INSERT INTO '.$config['mysql_prefix'].'messages(idt,idu,body,date) 
								VALUES("'.$idt.'","'.$_SESSION['idu'].'","'.$body.'","'.date('Y-m-d').'")');
						
						$id_rep = mysql_insert_id();

						mysql_query('UPDATE '.$config['mysql_prefix'].'forums SET nbm=nbm+1 
								WHERE id='.htmlentities($_GET['idf']).'');
	
						mysql_query('UPDATE '.$config['mysql_prefix'].'users_infos SET nbm=nbm+1 
								WHERE idu='.$_SESSION['idu'].'');
	
						mysql_query('UPDATE '.$config['mysql_prefix'].'topics SET nbm=nbm+1,uptime="'.time().'" 
								WHERE id='.$idt.'');
	
						$result .= '<script language="javascript" type="text/javascript">
									window.location.replace("?p=topic&id='.$idt.'&page='.$page.'#'.$id_rep.'");
								</script>';
					}
				}
			} else if($_GET['action'] == 'sondage') {
	
			}
		} else {
			if($_GET['action'] == 'topic') {
				$title = mysql_query('SELECT title FROM '.$config['mysql_prefix'].'forums 
								  WHERE id='.htmlentities($_GET['id']).'');

				$title = mysql_fetch_array($title);

				$result = '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.htmlentities($_GET['id']).'">'.$title['title'].'</a> >> '._('new_topic').'</h4>';

				$result .= '<fieldset>
						<form method="post" action="?p=post&amp;action=topic&amp;id='.htmlentities($_GET['id']).'&amp;send=ok" enctype="multipart/form-data">
							<label>'._('titre').' <br />
								<input size="70" maxlength="70" tabindex="1" name="title" /> <br />
							</label>
							<label>'._('message').' <br />
								<textarea rows="20" cols="95" tabindex="2" name="body"></textarea>
							</label><br />
							<label>'._('status').'<br />
								<table>
									<tr>
										<td>'._('post-it').'  </td><td><input type="checkbox"  value="100" name="post-it"/></td>
										<td>'._('locked').' </td><td><input type="checkbox" value="10" name="locked"/></td>
									</tr>
								</table>
							</label><br />
							<input type="submit" value="'._('okform').'" />
						</form>
						</fieldset>';
			} else if ($_GET['action'] == 'repondre') {
				$title = mysql_query('SELECT T.idf AS Fidf, F.title AS Ftitle, T.title AS Ttitle 
								  FROM '.$config['mysql_prefix'].'forums AS F,  '.$config['mysql_prefix'].'topics AS T 
								  WHERE T.id='.htmlentities($_GET['id']).' AND F.id=T.idf')or die(mysql_error());

				$title = mysql_fetch_array($title);
				$result = '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.$title['Fidf'].'">'.$title['Ftitle'].'</a> >> <a href="?p=topic&amp;id='.htmlentities($_GET['id']).'">'.$title['Ttitle'].'</a> >> '._('repondre').'</h4>';
				$result .= '<fieldset>
						<form method="post" action="?p=post&amp;action=repondre&amp;id='.htmlentities($_GET['id']).'&amp;idf='.$title['Fidf'].'&amp;send=ok" enctype="multipart/form-data">
							<label>'._('message').' <br />
								<textarea rows="20" cols="95" tabindex="2" name="body"></textarea>
							</label><br />
							<input type="submit" value="'._('okform').'" />
						</form>
						</fieldset>';
			} else if($_GET['action'] == 'sondage') {
	
			}
		}
	} else {
		$result = _('auth_erreur2');
	}
	return $result;
}
// Module de gestions des topics.
function mod_forum() 
{
	global $config;
	$id_forum = htmlentities($_GET['id']);

	// Pagination
	$nbn_req = mysql_query('SELECT COUNT(id)
				     FROM '.$config['mysql_prefix'].'topics 
				     WHERE idf="'.$id_forum.'"  AND status<100');
	$nbn = mysql_result($nbn_req,0);
	$pages = per_page($nbn,$_GET['page']);

	// Epinglés.
	$mysql = mysql_query('SELECT U.id AS Uid, idf, title, username, F.nbm AS Fnbm, date, F.id AS Tid, status
					     FROM '.$config['mysql_prefix'].'topics AS F, '.$config['mysql_prefix'].'users AS U, '.$config['mysql_prefix'].'users_infos AS I
					     WHERE idf="'.$id_forum.'" AND F.idu=U.id AND I.idu=U.id  AND status>=100 ORDER BY F.uptime DESC');
	$nbp = mysql_num_rows($mysql);

	$title = mysql_query('SELECT title FROM '.$config['mysql_prefix'].'forums 
					  WHERE id='.$_GET['id'].'');
	$title = mysql_fetch_array($title);

	$result .= '<div class="right">
				<a href="?p=post&amp;action=topic&amp;id='.$_GET['id'].'">'._('new_topic').'</a> - <a href="?p=post&amp;action=sondage&amp;id='.$_GET['id'].'">'._('new_sond').'</a>
			  </div>';
	$result .= '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> '.$title['title'].'</h4>';
	$result .= $pages[2];
	if($nbp) {
		$result .= '<table class="tab">
					<tr>
						<th>'._("titre").'</th>
						<th>'._("reponses").'</th>
						<th>'._("dernier").'</th>
					</tr>';
		while($forums = mysql_fetch_array($mysql)) {
			$date = explode('-',$forums['date']);
			$date = $date[2].'/'.$date[1].'/'.$date[0];
			$result .= '<tr>
						<td class="tab_title_1">
							<h2>'.check_status_topic($forums['status']).'<a href="?p=topic&amp;id='.$forums['Tid'].'" name="'.$forums['title'].'">'.$forums['title'].'</a></h2>
							<div class="small">'._('le').' '.$date.' par <a href="?p=view&amp;id='.$forums['Uid'].'" name="'.$forums['username'].'">'.$forums['username'].'</a></div>
						</td>
						<td class="tab_other_2">
							'.$forums['Fnbm'].' <br />
						</td>
						<td class="tab_other_1">
							'. last_rep($forums['Tid']).'
						</td>
					</tr>';
		} 
	}
	// Sujets non épinglés.
	$mysql = mysql_query('SELECT U.id AS Uid, idf, title, username, F.nbm AS Fnbm, date, F.id AS Tid, status
				     FROM '.$config['mysql_prefix'].'topics AS F, '.$config['mysql_prefix'].'users AS U, '.$config['mysql_prefix'].'users_infos AS I
				     WHERE idf="'.$id_forum.'" AND F.idu=U.id AND I.idu=U.id  AND status<100 ORDER BY F.uptime DESC LIMIT '.$pages[1].','.$pages[0].'')or die(mysql_error());

	if($nbn || $nbp) {
		if(!$nbp) {
			$result .= '<table class="tab">
						<tr>
							<th>'._("titre").'</th>
							<th>'._("reponses").'</th>
							<th>'._("dernier").'</th>
						</tr>';
		}
		while($forums = mysql_fetch_array($mysql)) {
			$date = explode('-',$forums['date']);
			$date = $date[2].'/'.$date[1].'/'.$date[0];
			$result .= '<tr>
						<td class="tab_title_1">
							<h2>'.check_status_topic($forums['status']).'<a href="?p=topic&amp;id='.$forums['Tid'].'" name="'.$forums['title'].'">'.$forums['title'].'</a></h2>
							<div class="small">'._('le').' '.$date.' par <a href="?p=view&amp;id='.$forums['Uid'].'" name="'.$forums['username'].'">'.$forums['username'].'</a></div>
						</td>
						<td class="tab_other_2">
							'.$forums['Fnbm'].' <br />
						</td>
						<td class="tab_other_1">
							'. last_rep($forums['Tid']).'
						</td>
					</tr>';
	}
	$result .= '</table>';
	$result .= $pages[2];
	} else {
		$result .= _('nomsg');
	}
	$result .= '<div class="right">
			<a href="?p=post&amp;action=topic&amp;id='.$_GET['id'].'">'._('new_topic').'</a> - <a href="?p=post&amp;action=sondage&amp;id='.$_GET['id'].'">'._('new_sond').'</a>
		  </div><br /><br />';
	return $result;
}

// Module de gestions d'affichage des topics
function mod_topic()
{
	// Vars
	global $config;
	$i = 1;

	if(!$_GET['page']) $page = 1;
	else $page = $_GET['page'];
	$j = (($page-1)*($config['board_per_page']))+1;

	$id_forum = htmlentities($_GET['id']);

	// Pagination
	$nbp_req = mysql_query('SELECT COUNT(id)
					     FROM '.$config['mysql_prefix'].'messages
					     WHERE idt="'.$id_forum.'"');
	$nbp = mysql_result($nbp_req,0);

	// On récup les informations.
	$mysql = mysql_query('SELECT F.idf AS Fidf, U.id AS Uid, idf, title, username, F.nbm AS Fnbm, date, avatar, body, I.nbm AS Unbm, signature, birthday, sexe, location
					     FROM '.$config['mysql_prefix'].'topics AS F, '.$config['mysql_prefix'].'users AS U, '.$config['mysql_prefix'].'users_infos AS I
					     WHERE F.id="'.$id_forum.'" AND F.idu=U.id AND I.idu=U.id');
	
	// Title forum.
	$forums = mysql_fetch_array($mysql);
	$title = mysql_query('SELECT title 
					  FROM '.$config['mysql_prefix'].'forums 
					  WHERE id='.$forums['Fidf'].'');
	
	$title = mysql_fetch_array($title);
	
	$pages = per_page($nbp,$_GET['page']);
	if(!$_GET['page'] OR $_GET['page'] == 1) {
		// Date du post.
		$date = explode('-',$forums['date']);
		$date = $date[2].'/'.$date[1].'/'.$date[0];
	
		// Sexe & Age & Ville
		if($forums['birthday'] != '0000-00-00') {
			$anniv = explode('-',$forums['birthday']);
			$age = age_calc($anniv).' '._('ans').'<br />';
		}
		if($forums['sexe'] != -1) {
			if($forums['sexe']) {
				$sexe = _('femme').' ';
			} else {
				$sexe = _('homme').' ';
			}	
		}
		$location = $forums['location'].'<br />';
	
		// On  prépare le retour pour le sujet.
		$result .= '<div class="right">
					<a href="?p=post&amp;action=repondre&amp;id='.$_GET['id'].'">'._('repondre').'</a></a>
				</div>';
		$result .= '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.$forums['Fidf'].'">'.$title['title'].'</a> >> '.$forums['title'].'</h4>';
		$result .= $pages[2];
		$result .= '<table class="tab">
					<tr class="tr_top">
						<th>'._("auteur").'</th>
						<th>'._("messages").'</th>
					</tr>';
	
		if(empty($forums ['avatar'])) {
			$avatar = $config['dir_avatars'].'/noavatar.png';
		} else {
			$avatar = $config['dir_avatars'].'/'.$forums['Uid'].$forums['avatar'];
		}
	
		$result .= '<tr class="tr_top">
					<td class="tab_other_'.$i.'" style="vertical-align: top">
						<h2><a href="?p=view&amp;id='.$forums['Uid'].'" name="'.$forums['username'].'">'.$forums['username'].'</a></h2>
						<br />
						<img src="'.$avatar.'" alt="'.$avatar.'" /><br /><br />
						<div class="info_user">
							'._('messages').' : '.$forums['Unbm'].'<br />
							'.$location.$sexe.$age.'
						</div>
					</td>
					<td class="tab_title_'.$i.'" style="vertical-align:top">
						<div class="tr_top">
							<h3>[ '.$date.' ] - [ '.$forums['title'].' ]</h3>'.nl2br($forums['body']).' 
						</div>
						<br /><hr />
						<div class="tr_bottom">
							'.nl2br($forums['signature']).'
						</div>
					</td>
				</tr>';
	} else {
		// On  prépare le retour pour le sujet.
		$result .= '<div class="right">
					<a href="?p=post&amp;action=repondre&amp;id='.$_GET['id'].'">'._('repondre').'</a></a>
				</div>';
		$result .= '<h4><a href="'.$config['board_uri'].'">'.$config['board_title'].'</a> >> <a href="?p=forum&amp;id='.$forums['Fidf'].'">'.$title['title'].'</a> >> '.$forums['title'].'</h4>';
		$result .= $pages[2];
		$result .= '<table class="tab">
					<tr class="tr_top">
						<th>'._("auteur").'</th>
						<th>'._("messages").'</th>
					</tr>';
	}
	// On récup les infos.
	$mysql = mysql_query('SELECT U.id AS Uid, F.id AS Fid, username, avatar, body, I.nbm AS Unbm, signature, birthday, sexe, location, date
					     FROM '.$config['mysql_prefix'].'messages AS F, '.$config['mysql_prefix'].'users AS U, '.$config['mysql_prefix'].'users_infos AS I
					     WHERE F.idt="'.$id_forum.'" AND F.idu=U.id AND I.idu=U.id ORDER BY F.id ASC LIMIT '.$pages[1].','.$pages[0].'');

	// On  prépare le retour pour les réponses.
	while($forums = mysql_fetch_array($mysql)) {
		$i++;
		// Date du post.
		$date = explode('-',$forums['date']);
		$date = $date[2].'/'.$date[1].'/'.$date[0];
	
		// Sexe & Age & Ville
		if($forums['birthday'] != '0000-00-00') {
			$anniv = explode('-',$forums['birthday']);
			$age = age_calc($anniv).' '._('ans').'<br />';
		}
		if($forums['sexe'] != -1) {
			if($forums['sexe']) {
				$sexe = _('femme');
			} else {
				$sexe = _('homme');
			}	
		}
		$location = $forums['location'].'<br />';

		if(empty($forums ['avatar'])) {
			$avatar = $config['dir_avatars'].'/noavatar.png';
		} else {
			$avatar = $config['dir_avatars'].'/'.$forums['Uid'].$forums['avatar'];
		}

		$result .= '<tr class="tr_top">
					<td class="tab_other_'.$i.'" style="vertical-align: top">
					<h2><a href="?p=view&amp;id='.$forums['Uid'].'" name="'.$forums['username'].'">'.$forums['username'].'</a></h2>
					<br />
					<img src="'.$avatar.'" alt="'.$avatar.'" /><br /><br />
					<div class="info_user">
						'._('messages').' : '.$forums['Unbm'].'<br />
						'.$location.$sexe.$age.'
					</div>
				</td>
				<td class="tab_title_'.$i.'" style="vertical-align:top">
					<div class="tr_top">
						<h3>[ '.$date.' ] - [ <a name="'.$forums['Fid'].'"></a><a href="#'.$forums['Fid'].'" >#'.$j.'</a> ]</h3>
						'.nl2br($forums['body']).' 
					</div>
					<br /><hr />
					<div class="tr_bottom">
						 '.nl2br($forums['signature']).'
					</div>
				</td>
			</tr>';
		if($i >= 2) {
			$i = 0;
		}
		$j++;
	}
	$result .= '</table>';
	$result .= $pages[2];
	$result .= '<div class="right">
				<a href="?p=post&amp;action=repondre&amp;id='.$_GET['id'].'">'._('repondre').'</a></a>
			  </div><br /><br />';

	return $result;
}

// Module de gestion de la liste des forums.
function mod_forums() 
{
	global $config;
	$i = 1;
	$last_i = 0;
	
	$mysql = mysql_query('SELECT COUNT(id) 
					      FROM '.$config['mysql_prefix'].'groups');

	$groups = mysql_result($mysql,0);
	
	while($i <= $groups) {
		$mysql = mysql_query('SELECT F.idg, F.id, F.title AS FTitle, F.body, F.nbt, F.nbm, F.idl, G.title AS GTitle 
						     FROM '.$config['mysql_prefix'].'forums as F, '.$config['mysql_prefix'].'groups as G 
						     WHERE F.idg='.$i.' AND G.id=F.idg');
							
		while($forums = mysql_fetch_array($mysql)) {
			if($i != $last_i) {
				$result .= '<h3>'.$forums['GTitle'].'</h3>';
				$result .= '<table class="tab">
								<tr>
									<th>'._("titre").'</th>
									<th>'._("discussions").' / '._("messages").'</th>
									<th>'._("dernier").'</th>
								</tr>';
				$last_i = $i;
			}	
			$result .= '<tr>
							<td class="tab_title_1">
								<h2><a href="?p=forum&amp;id='.$forums['id'].'" name="'.$forums['FTitle'].'">'.$forums['FTitle'].'</a></h2> 
								'.$forums['body'].'
							</td>
							<td class="tab_other_2">
								'.$forums['nbt'].' / '.$forums['nbm'].'
							</td>
							<td class="tab_last_1">
								'.last_topic($forums['id']).'
							</td>
						</tr>';
		}
		$result .= '</table><br />';
		$i++;
	}
	$result .= '<h3>'._('statistiques').'</h3>';
	$result .= load_stats();
	$result .= '<br />';
	$result .= load_online();

	return $result;
}

?>